//贷款id,客户id
var loanId,clientId;
//初始化表单
var initForm = function(){
     
     //初始化附件表单
     $('#inspectForm').form({
 		url :'sys/SysAccessoryExt/upload',
 		onBeforeLoad : function(param){
 			$.messager.progress();
 		},
 		onLoadSuccess : function(data) {
 			//加载完成之后才构建
 			$.messager.progress("close");
 		},
 		onLoadError : function(data) {
 			//加载完成之后才构建
 			$.messager.progress("close");
 			$.dialog.alert("加载内容失败,请重新刷新页面/联系管理员");
 		},
 		onSubmit : function(param) {
 			if (!$(this).form("validate")) {
 				$.dialog.alert("提交的选项中,有错误内容,请检查");
 				return false;
 			}
 			$.messager.progress();
 		},
 		success : function(data) {	
 			$.messager.progress("close");
 			var jd = eval('(' + data + ')');
 			if(!!!jd){
 				$.dialog.alert("未知异常");
 				return false;
 			}
 			if(jd.state!=200){
 				$.dialog.alert(jd.message);
 				return false;
 			}
 			$.dialog({
 				icon: 'success.gif',
 				title: '提示',
 			    content: '操作成功！',
 			    ok: function(){
 			        this.close();
 			    }
 			});
 			$('#inspectTable').dataTable().fnDraw();
 			
 		}
 	});
     
   //初始化考察表单
     $('#_inspect_detail').form({
 		url :'sys/LoanInspect/addOrUpdate',
 		onBeforeLoad : function(param){
 			$.messager.progress();
 		},
 		onLoadSuccess : function(data) {
 			//加载完成之后才构建
 			$.messager.progress("close");
 		},
 		onLoadError : function(data) {
 			//加载完成之后才构建
 			$.messager.progress("close");
 			$.dialog.alert("加载内容失败,请重新刷新页面/联系管理员");
 		},
 		onSubmit : function(param) {
 			if (!$(this).form("validate")) {
 				$.dialog.alert("提交的选项中,有错误内容,请检查");
 				return false;
 			}
 			$.messager.progress();
 		},
 		success : function(data) {	
 			$.messager.progress("close");
 			var jd = eval('(' + data + ')');
 			if(!!!jd){
 				$.dialog.alert("未知异常");
 				return false;
 			}
 			if(jd.state!=200){
 				$.dialog.alert(jd.message);
 				return false;
 			}
 			$("#inspectId").val(data.id);
 			$.dialog({
 				icon: 'success.gif',
 				title: '提示',
 			    content: '保存考察记录成功!',
 			    ok: function(){
 			        this.close();
 			    }
 			});
 		}
 	});
};


//初始化列表
var initDataTable = function(){
	
	//附件列表初始化
	$('#inspectTable').dataTable({
		"bProcessing" : true, //DataTables载入数据时，是否显示‘进度’提示
        "bServerSide" : true, //是否启动服务器端数据导入
        "bStateSave" : false, //是否打开客户端状态记录功能,此功能在ajax刷新纪录的时候不会将个性化设定回复为初始化状态
        "bJQueryUI" : true, //是否使用 jQury的UI theme
        "aLengthMenu" : [5, 10, 15], //更改显示记录数选项
        "iDisplayLength" : 5, //默认显示的记录数
        "bAutoWidth" : true, //是否自适应宽度 update\
        "bScrollCollapse" : false, //是否开启DataTables的高度自适应，当数据条数不够分页数据条数的时候，插件高度是否随数据条数而改变
        "bPaginate" : true, //是否显示（应用）分页器
        "bInfo" : true, //是否显示页脚信息，DataTables插件左下角显示记录数
        "sPaginationType" : "full_numbers", //详细分页组，可以支持直接跳转到某页
        "bSort" : true, //是否启动各个字段的排序功能
        "aaSorting" : [[1, "DESC"]], //默认的排序方式，第3列，升序排列
        "bFilter" : false, //是否启动过滤、搜索功能
        "sScrollX": "100%",
		"sScrollXInner": 1675,
        "sPaginationType": "full_numbers",
	    "sAjaxSource":'sys/SysAccessory/list',
        "fnServerData":inspectRetrieveData,  //自定义数据获取函数
		"aoColumns" : [ {
			"mDataProp" : "uid",
			"sTitle" : "操作",
			"bSortable": false,
			"sDefaultContent" : "",
			"sClass" : "center",
			"mRender": function (data, display, row) {
		      return  ( '<a href="javascript:down_load('+row.id+');" class="tablelink">'+'下载'+'</a>'+'&nbsp;&nbsp;'+'<a href="javascript:del_default('+row.id+');" class="tablelink">'+'删除'+'</a>');
		    }
			
		}, {
			"mDataProp" : "accessoryTheme",
			"sTitle" : "主题",
			"sDefaultContent" : "",
			"sClass" : "center"
		}, {
			"mDataProp" : "accessoryClass",
			"sTitle" : "附件类型",
			"sDefaultContent" : "",
			"sClass" : "center",
			"mRender": function (data, display, row) {
		        return SYS_BASE_DATA['Accessory_Class_Inspect_Code'][data]||'';
		     }
		}, {
			"mDataProp" : "accessoryName",
			"sTitle" : "文件名",
			"sDefaultContent" : "",
			"sClass" : "center",
			"mRender": function (data, display, row) {
		        return '<a href="javascript:down_load('+row.id+');">'+data+'</a>';
		     }
		}, {
			"mDataProp" : "addUser",
			"sTitle" : "上传人",
			"sDefaultContent" : "",
			"sClass" : "center",
			"mRender": function (data, display, row) {
		        return data||'ADMIN';
		     }
		}, {
			"mDataProp" : "addTime",
			"sTitle" : "上传时间",
			"sDefaultContent" : "",
			"sClass" : "center"
		}, {
			"mDataProp" : "accessoryRemark",
			"sTitle" : "备注",
			"sDefaultContent" : "",
			"sClass" : "center"
			
		}
		  
		],
		"oLanguage": {
            "sUrl":"static_resources/language/lang-cn.txt" //这个是国际化，也就是汉化的各种文字数据源，写对url就行了
        }
        
	});
};

//自定义数据获取函数
var inspectRetrieveData = function( sSource, aoData, fnCallback ) {
	   aoData.push({'name':'beanProxy[clientId]','value':clientId});
	   aoData.push({'name':'beanProxy[loanId]','value':loanId});
	   aoData.push({'name':'beanProxy[enable]','value':0});
	   aoData.push({'name':"v2hldghlcib0agugzgf0ysbhy2nlc3m",value:1});
	   aoData.push({'name':"beanProxy[ownerType]",value:'KC'});
	        $.ajax( {
	                type: "POST", 
	                url: sSource, 
	                cache : false,
	                dataType : "json",
	                data : aoData, 
	                success: function(resp) {
	                	if(resp && resp.state == 200){
	                		fnCallback(resp.datagrid);
	                		return false;
	                	}
	                	fnCallback(null);
	            }
	        });
};

//初始化数据
var initFormData = function(){
	if(_is_not_empty(loanId)){
		$('#_inspect_detail').form('load', 'sys/LoanInspect/find?loanId=' + loanId);
		
	}
};

//退回考察
var returnLoan = function(){
	$.dialog.confirm('你确定退回这条申请单吗？', function(){
		$.ajax({
	       	url:'sys/LoanExt/back',
	    	data:{id:loanId,toStatus:2},
	    	type:'post',
	    	dataType:'json',
	    	success:function(result){
	    	        if(result.state == "200"){
	    	        	$.dialog({
	    	 				icon: 'success.gif',
	    	 				title: '提示',
	    	 			    content: '退回考察单成功！',
	    	 			    ok: function(){
	    	 			    	location.href = 'jsp/loan/inspectLoanList.jsp';
	    	 			    }
	    	 			});
	    	        }else{
	    	        	$.dialog.alert('退回申请单失败！');
	    	        }
	    		}
	        });
	}, function(){
//	    $.dialog.tips('执行取消操作');
	});
};

//提交考察
var submitInspect = function(){
	$.ajax({
       	url:'sys/LoanExt/submitStatus',
    	data:{id:loanId,loanStatus:2,loanInspectTime:top.nowTime,loanInspectName:loginUser.userFullName,loanInspect:loginUser.id,inspectStatus:1,returnStatus:0},
    	type:'post',
    	dataType:'json',
    	success:function(result){
    	        if(result.state == "200"){
    	        	$.dialog({
    	 				icon: 'success.gif',
    	 				title: '提示',
    	 			    content: '提交考察成功！',
    	 			    ok: function(){
    	 			    	location.href = 'jsp/loan/inspectLoanList.jsp';
    	 			    }
    	 			});
    	        }else{
    	        	$.dialog.alert('提交调查失败！');
    	        }
    		}
        });
};
//删除附件
var del_default = function(id){
	$.dialog.confirm('你确定删除这个附件吗？', function(){
		$.ajax({
	       	url:'sys/SysAccessory/delete',
	    	data:{pks:id},
	    	type:'post',
	    	dataType:'json',
	    	success:function(result){
	    	        if(result.state == "200"){
	    	        	$.dialog({
	    	 				icon: 'success.gif',
	    	 				title: '提示',
	    	 			    content: '删除成功！',
	    	 			    ok: function(){
	    	 			        this.close();
	    	 			    }
	    	 			});
	    	        	$('#inspectTable').dataTable().fnDraw();
	    	        }else{
	    	        	$.dialog.alert('删除失败！');
	    	        }
	    		}
	        });
	});
};
//init操作
$(function(){
	loanId = $('#loanId').val();
	clientId = $('#clientId').val();
	initForm();
	initFormData();
	initDataTable();
	
	$('#accessoryClass').mSelect({
		myClass:'easyui-validatebox dfinput',
		width:192,
		baseCode:'Accessory_Class_Inspect_Code',
		isAllSelect:false,
		dataOptions:'required:true'
	});
	
	//给详情页面赋值
	_set_default_lable_text('sys/LoanInspect/find?loanId=' +loanId,false);
	
});
